package com.nextbusmn.server.JDOWrappers;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.nextbusmn.server.dataobjects.BusTripData;
import com.nextbusmn.server.dataobjects.NextBusData;
import com.nextbusmn.server.dataobjects.StopTimeData;

public class BusTripJDOWrapper extends JDOWrapper {

	public BusTripJDOWrapper(BusTripData data) {
		this.setData(data);
	}
	
	@Override
	public boolean dataExists(NextBusData data) {
		BusTripData id = getByTripID(((BusTripData) data).getTripID());
		return id != null;
	}

	public static BusTripData getByID(String ID) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		return (BusTripData) pm.getObjectById(BusTripData.class, ID);
	}
	
	public static BusTripData getByTripID(String ID) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		Query query = pm.newQuery(BusTripData.class);
		query.setFilter("tripID == IDParam");
		query.declareParameters("String IDParam");

		List<BusTripData> result = null;
		
		try {
			result = (List<BusTripData>) query.execute(ID);
		} finally {
			query.closeAll();
		}
		
		if (result.size() > 1) {
			System.out.println("Data problem: Key not unique!"); 
		}
		
		return result.size() == 1 ? result.get(0) : null;
	}
	
	public static List<BusTripData> getTripsByRouteID(String routeID) {
		
PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		Query query = pm.newQuery(BusTripData.class);
		query.setFilter("routeID == rIDParam");
		query.declareParameters("String rIDParam");
		
		List<BusTripData> result = null;
		List<BusTripData> detached = null;
		
		try {
			result = (List<BusTripData>) query.execute(routeID);
			for (BusTripData d : result) {
				d.getStopTimes();
			}
			detached = (List<BusTripData>) pm.detachCopyAll(result);
		} finally {
			query.closeAll();
		}
		
		return detached;
		
	}
	
	public static List<BusTripData> getAllRows() {
		return (List<BusTripData>) JDOWrapper.getAllRows(BusTripData.class);
	}
	
	public static void update(BusTripData tripData) {
		//TODO implement this
	}

	public static List<BusTripData> getBusTripDataByParentId( Key user_key){
	    PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();  
	    Query query = pm.newQuery("select from "+BusTripData.class.getName()+" WHERE busRoute == keyParam");
	    query.declareParameters(" String k");
	   // query.setUnique(true);
	    List<BusTripData> theData = (List<BusTripData>) query.execute(user_key.getId());
	    System.out.println(theData.size());
	    return theData;
	}
	
	public static List<BusTripData> getByRouteKey(Key key) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		List<BusTripData> finalList = new ArrayList<BusTripData>();
		
		Query query = pm.newQuery(BusTripData.class); 
		query.setFilter("busRoute == parentKey");
	    //query.setOrdering("stopSequence asc");
	    query.declareParameters("String parentKey");
	    
	    try {
	    	finalList = (List<BusTripData>) query.execute(key);
	    } finally {
	    	query.closeAll();
	    }
		return finalList;
	}
}
